//
//  IMChatMainViewController.m
//  ChatMainDemo
//
//  Created by peiw on 16/1/21.
//  Copyright © 2016年 peiw. All rights reserved.
//

#import "IMChatMainViewController.h"
#import "IMChatProtocol.h"
#import "IMChatDisplayData.h"
#import "IMCellDisplayData.h"
#import "IMBaseCell.h"
#import "IMCellFactory.h"

@interface IMChatMainViewController ()<UITableViewDataSource , UITableViewDelegate>

@property (nonatomic , strong) UITableView *tableView;


@property (nonatomic , strong) IMChatDisplayData *data;

@end

@implementation IMChatMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.eventHandle updateView];
}


#pragma mark - UI
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource , UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IMBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chat"];
    IMCellDisplayData *display = self.data.rows[indexPath.row];
    if (!cell) {
        IMCellFactory *factory = [IMCellFactory new];
        cell = [factory cellForType:display.msg.msgType identifier:@"chat"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setContent:display];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    IMCellDisplayData *display = self.data.rows[indexPath.row];
    return MAX(display.contentSize.height, 44);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    IMCellDisplayData *display = self.data.rows[indexPath.row];
    return MAX(display.contentSize.height, 44);
}

#pragma mark - IMChatViewInterface

- (void)showUpcomingDisplayData:(IMChatDisplayData *)display {
    self.data = display;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
